package router

import (
	"github.com/iris-contrib/middleware/cors"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/middleware/recover"
	businessRouter "wit-admin-go/business/router"
	"wit-admin-go/conf"
	"wit-admin-go/system/app/controller"
)

// PublicUrl 放行路由
var PublicUrl []string

func RegisterRouter(app *iris.Application) {
	// 跨域解决方案
	crs := cors.New(cors.Options{
		AllowedOrigins:   []string{"*"},
		AllowedMethods:   []string{"HEAD", "GET", "POST", "PUT", "PATCH", "DELETE"},
		AllowCredentials: true,
		AllowedHeaders:   []string{"*"},
		Debug:            false,
	})
	app.UseRouter(recover.New(), crs)
	// 不需要验证的路由
	main := app.Party("/wit")
	{
		// 用户
		main.Post("/user/createUser", controller.User.CreateUser).Name = "创建用户/注册用户"
		main.Post("/user/login", controller.User.LogIn).Name = "登录"
		// 日志
		main.Post("/log/createLog", controller.Log.CreateLog).Name = "添加日志"
		//	同步Api数据
		main.Get("/apis/synApis", controller.Apis.SynApis).Name = "同步Api数据"
	}
	// 放行路由
	PublicUrl = append([]string{
		"/wit/user/login",
		"/wit/user/createUser",
		"/wit/log/createLog",
		"/wit/apis/synApis",
		conf.CONFIG.WitAdmin.WebSocketPath,
	}, businessRouter.BusinessPublicUrl...)
	// 路由
	User(main)
	Log(main)
	Role(main)
	Apis(main)
	Menu(main)
	WitCasbin(main)
	// 业务路由
	businessRouter.BusinessRouter(main)
}
